package xin.clips.system.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import xin.clips.common.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * LogUtil - 日志记录工具类
 *
 * @author author
 * @version 0.0.1
 * @since 2021-09-30
 */
public class LogUtil {

    //定义一个全局的记录器，通过LoggerFactory获取
    private final static Logger logger = LoggerFactory.getLogger(LogUtil.class);

    /**
     * 输出info日志
     *
     * @param context 日志内容
     */
    public static void i(String context) {
        logger.info(context);
    }

    /**
     * 输出debug日志
     *
     * @param context 日志内容
     */
    public static void d(String context) {
        logger.debug(context);
    }

    /**
     * 输出warn日志
     *
     * @param context 日志内容
     */
    public static void w(String context) {
        logger.warn(context);
    }

    /**
     * 输出error日志
     *
     * @param context 日志内容
     */
    public static void e(String context) {
        logger.error(context);
    }

    /**
     * 获取异常详细堆栈信息
     */
    public static void dumpException(Throwable e) {
        logger.error(getTrace(e));
    }

    /**
     * 获取异常详细堆栈信息
     */
    public static void dumpException(String e) {
        logger.error(e);
    }

    /**
     * 获取异常详细堆栈信息
     *
     * @param e         异常
     * @param maxLength 最长有效位数
     */
    public static String getTrace(Throwable e, int maxLength) {
        StringBuffer msg = new StringBuffer("null");
        if (e != null) {
            msg = new StringBuffer("");
            String message = e.toString();
            int length = e.getStackTrace().length;
            if (length > 0) {
                msg.append(message).append("\n");
                for (int i = 0; i < length; i++) {
                    msg.append("\t").append(e.getStackTrace()[i]).append("\n");
                }
            } else {
                msg.append(message);
            }
        }
        return StringUtils.substring(msg.toString(), maxLength);
    }

    /**
     * 获取异常详细堆栈信息
     */
    public static String getTrace(Throwable e) {
        return getTrace(e, -1);
    }

    public static void appLog(String log) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return;
        }
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        HttpSession session = request.getSession(true);
        LogUtil.i("APP[" + session.getId() + "] ==> " + log);
    }
}
